package cn.yh.register.hosp.api;

import cn.yh.register.common.exception.RegisterException;
import cn.yh.register.common.helper.HttpRequestHelper;
import cn.yh.register.common.result.Result;
import cn.yh.register.common.result.ResultCodeEnum;
import cn.yh.register.hosp.service.DepartmentService;
import cn.yh.register.hosp.service.HospitalService;
import cn.yh.register.hosp.service.HospitalSetService;
import cn.yh.register.hosp.service.ScheduleService;
import cn.yh.register.model.hosp.Department;
import cn.yh.register.model.hosp.Hospital;
import cn.yh.register.model.hosp.Schedule;
import cn.yh.register.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aopalliance.intercept.Interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.naming.spi.DirStateFactory;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

//平台对外开发的接口都写在该Controller类  向外界提供医院上传接口
@RestController
@Api(tags = "医院管理API接口")
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService setService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    //提供上传医院的接口  供其他医院调用
    @ApiOperation("上传医院")
    @PostMapping("/saveHospital")
    public Result<?> saveHospital(HttpServletRequest request){
       //使用封装好的handleParam方法
        Map<String, Object> objectMap = this.handleParam(request);
        //调用service
        hospitalService.saveHospital(objectMap);
        return Result.ok();
    }

    //提供查询医院接口
    @ApiOperation("查询医院")
    @PostMapping("/hospital/show")
    public Result<Hospital> show(HttpServletRequest request){
        //校验签名key的操作使用封装好的方法并获取到数据
        Map<String, Object> objectMap = this.handleParam(request);
        //调用service层
       Hospital hospital =  hospitalService.show(objectMap);
       return Result.ok(hospital);
    }

    //上传科室接口
    @ApiOperation("上传科室")
    @PostMapping("/saveDepartment")
    public Result<?> saveDepartment(HttpServletRequest request){
        //封装的方法取数据校验签名key
        Map<String, Object> objectMap = this.handleParam(request);
        //调用service
        departmentService.saveDepartment(objectMap);
        return Result.ok();
    }

    @ApiOperation("分页查询科室列表")
    @PostMapping("/department/list")
    public Result<?> list(HttpServletRequest request){
        //校验签名key  自定义的封装方法
        Map<String, Object> objectMap = this.handleParam(request);
        String hoscode = (String) objectMap.get("hoscode");
        Integer page = StringUtils.isEmpty(objectMap.get("page")) ? 1 : Integer.parseInt((String)objectMap.get("page"));
        Integer limit = StringUtils.isEmpty(objectMap.get("limit")) ? 10 : Integer.parseInt((String)objectMap.get("limit"));
        Page<Department> deptPage = departmentService.getPageList(hoscode,page,limit);
        return Result.ok(deptPage);
    }

    @ApiOperation("删除科室")
    @PostMapping("/department/remove")
    public Result<?> remove(HttpServletRequest request){
        //校验签名key
        Map<String, Object> objectMap = this.handleParam(request);
        //拿到需要的参数
        String hoscode = (String) objectMap.get("hoscode");
        String depcode = (String) objectMap.get("depcode");
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    @ApiOperation("上传排班")
    @PostMapping("/saveSchedule")
    public Result<?> saveSchedule(HttpServletRequest request){
        //取数据  校验签名key
        Map<String, Object> objectMap = this.handleParam(request);
        scheduleService.saveSchedule(objectMap);
        return Result.ok();
    }

    @ApiOperation("分页查询排班")
    @PostMapping("/schedule/list")
    public Result<?> getPageList(HttpServletRequest request){
        //获取参数 并校验签名key
        Map<String, Object> objectMap = this.handleParam(request);
        //获取到hoscode  按照医院编号去查
        String hoscode = (String) objectMap.get("hoscode");
        ScheduleQueryVo queryvo = new ScheduleQueryVo();
        //把医院编号组装到queryvo中，因为需要接收的参数是queryvo
        queryvo.setHoscode(hoscode);
        //获取到分页page
        Integer page= StringUtils.isEmpty(objectMap.get("page")) ? 1 :  Integer.parseInt((String) objectMap.get("page"));
        //获取到limit
        Integer limit =StringUtils.isEmpty(objectMap.get("limit")) ? 1 : Integer.parseInt((String) objectMap.get("limit"));
        Page<Schedule> schedulePage = scheduleService.getScheduleList(queryvo,page,limit);
        return Result.ok(schedulePage);
    }

    @ApiOperation("删除排班")
    @PostMapping("/schedule/remove")
    public Result<?> removeSchedule(HttpServletRequest request){
        //获取信息并校验签名key
        Map<String, Object> objectMap = this.handleParam(request);
        //根据什么删？hoscode和hosScheduleId
        //取出hoscode
        String hoscode = (String) objectMap.get("hoscode");
        //取出hosScheduleId
        String hosScheduleId = (String) objectMap.get("hosScheduleId");

        //调用service
        scheduleService.removeSchedule(hoscode,hosScheduleId);
        return Result.ok();

    }



    /**
     * 封装取数据 、校验签名key的方法
     * @param request
     * @return
     */
    private Map<String, Object> handleParam(HttpServletRequest request){
        //取出数据放到map中
        Map<String, String[]> paramMap = request.getParameterMap();
        //值转成对象对象形式
        Map<String, Object> objectMap = HttpRequestHelper.switchMap(paramMap);
        //根据医院的hoscode取出医院数据
        String hoscode = (String) objectMap.get("hoscode");
        //判断 如果hoscode为空的话 说明医院还没有注册
        if (StringUtils.isEmpty(hoscode)){
            throw new RegisterException(ResultCodeEnum.PARAM_ERROR);
        }
        // 传输过程中logoData中的"+"被转成了" ",因此我们要再替换回去(将所有的" "转为"+")
        String logoData = (String) objectMap.get("logoData");
        if (!StringUtils.isEmpty(logoData)) {
            logoData = logoData.replaceAll(" ", "+");
            objectMap.put("logoData",logoData);
        }
        //根据hoscode开始取医院的签名key（这个签名key是从我们自己这边取的）
        String signKey = setService.getSignKey(hoscode);
        //取到签名key后，生成签名 调用工具类封装的方法  需要进行一个判断
        if (!HttpRequestHelper.isSignEquals(objectMap, signKey)) {
            //如果签名为空的话 抛出签名错误的异常
            throw new RegisterException(ResultCodeEnum.SIGN_ERROR);
        }
        return objectMap;
    }
}
